import { config } from '../config.js';
import { config as rootConfig } from '../../../config.js';
import { MyModel } from '../../../modules/my.js';
import { Common } from '../../my/common/common.js';
import { HomeModel } from '../../../modules/home.js';
import { config as _rootConfig } from '../../../project.config.js';

const app = getApp();
const my = new MyModel();
const common = new Common();
const home = new HomeModel();

Page({
  data: {
    rid: 0,
    live: false,  // 是否已收藏
    deliveryStatus: false, // 是否已投递
    live_src: "/images/home/jobdetails/icon_zwxq_sc.png",
    live_active: "/images/home/jobdetails/icon_zwxq_sc_active.png",
    recruitment: null, // 用来存职位详情的数据
    payMoney: config.data.salaryRange, // 薪资模板
    educationRequire: config.data.edu, // 学历模板
    welfareTreatment: config.data.checkboxItems, // 福利模板
    experienceRequire: config.data.experience, // 经验模板
    PATTERN:  '',
    loading: false
  },
  // 拨打电话
  callPhone: function(event) {
    if(this.data.deliveryStatus) {
      wx.makePhoneCall({
        phoneNumber: event.currentTarget.dataset.phone
      })
    }else {
      wx.showModal({
        content: '拨打电话需要投递一份简历哦'
      });
    }
  },
  // 点击收藏
  Collection(e) {
    let id = e.currentTarget.dataset.rid;
    app.isAuthize()
    .then(res => {
      if(res) {
        common.hasResume().then(res => {
          if(res) {
            if(this.data.live) {
              // 取消收藏
              home.unLives(id)
              .then(res => {
                if(res.code == 2) {
                  this.setData({
                    live: false
                  });
                }else {
                  wx.showModal({
                    content: '收藏失败'
                  });
                }
              })
            }else {
              // 收藏
              home.lives(id)
              .then(res => {
                if(res.code == 2) {
                  this.setData({
                    live: true
                  });
                }else {
                  wx.showModal({
                    content: '收藏失败'
                  });
                }
              })
            }
          }
        }) 
      }else {
        app.showAuthize()
      }
    })
  },
  // 投递简历
  deliveryResume() {
    app.isAuthize()
    .then(res => {
      if(res) {
        wx.showModal({
          title: this.data.recruitment.positionName,
          content: '是否投递该职位?',
          success: res => {
            if(res.confirm) {
              common.hasResume().then(res => {
                if(res) {
                  home.deliveryResume(this.data.rid)
                  .then(res => {
                    if(res.code == 2) {
                      this.setData({
                        deliveryStatus: true
                      })
                      wx.showToast({
                        title: '投递成功！',
                        icon: 'none'
                      });
                    }else {
                      wx.showToast({
                        title: '投递失败！',
                        icon: 'none'
                      });
                    }
                    if(res.code == 1) {
                      wx.showToast({
                        title: '投递失败!职位七天内只能投递一次',
                        icon: 'none'
                      });
                    }
                  })
                  .catch(err => {
                    wx.showToast({
                      title: '投递失败！',
                      icon: 'none'
                    });
                  })
                }
              })
            }
          }
        })
      }else {
        app.showAuthize()
      }
    })
  },
  // 查看当前职位是否收藏
  eachLives() {
    home.getAllLive()
    .then(res => {
      if(res.data == null) {
        this.setData({
          live: false
        })
      }else {
        let arr = res.data.recruitmentIds;
        arr.forEach(element => {
          if(this.data.rid == element) {
            this.setData({
              live: true
            })
          }
        });
      }
    })
  },
  // 查看职位是否投递
  isDeliveryLogs() {
    let data = {
      recruitmentId: this.data.rid,
      rid: common.getRid()
    }
    my.getOneResume(data)
    .then(res => {
      if(res.code == 2) {
        let time = res.data.receiveResume['create_time'];
        if((new Date().getTime() - time) / (1000*3600*24) > 7) {
          this.setData({
            deliveryStatus: false
          })
        }else {
          this.setData({
            deliveryStatus: true
          })
        }
      }
    })
  },
  onLoad: function (options) {
    let id = options.rid;
    this.setData({
      rid: id,
      PATTERN: _rootConfig.PATTERN
    });
    // 职位详情
    home.jobDetails(id)
    .then(res => {
      this.setData({
        recruitment: res.data.recruitment,
        loading: true
      });
    })
    this.eachLives();
    this.isDeliveryLogs()
  },
  onShareAppMessage: function (ops) {
    let _this = this;
    if (ops.from === 'button') {
      console.log(ops.target)
    }
    return {
      title: rootConfig.SHARE_TITLE,
      path: '/pages/home/jobdetails/index?rid=' + _this.data.rid,
      success: res => {
        console.log("转发成功:" + JSON.stringify(res));
      },
      fail: err => {
        console.log("转发失败:" + JSON.stringify(err));
      }
    }
  }
})